# coding = utf-8
import json
import xlwt
import os

# 遍历指定目录中的json文件
def File_find(path):
    film = os.listdir(path)
    file_json_list = []
    for each in film :
        if ".json" in each :
            file_json_list.append(each)
    # print(file_json_list)
    return file_json_list

# 读取json文件
def Read_json(path):
    data_list = []
    for each in File_find(path) :
        with open(each, "r",encoding='utf-8') as i :
            data = json.load(i)
        data_list.append(data)
    # print(data_list[0])
    return data_list

# 设置保存的excel文件名字
def Save_excle(path):
    json_read_name = File_find(path)
    excel_save_name = []
    for i in json_read_name :
        j = i.split(".",2)
        excel_save_name.append(j[0])
    return excel_save_name

# 转变为excel文件
def Convert_to_excel(path):
    json = Read_json(path)
    excel_name_list = Save_excle(path)
    film_path = os.chdir("D:/hello Python/邓老师任务/test1/result/xls")  # 老师，您运行程序时需要修改这个目录
    # print(json[5])
    # print(list(json[5].values())[3])
    # 每一个循环建立每一个新的excel
    excel_num = 0
    for each in json:
        each_book = xlwt.Workbook(encoding='utf-8')
        each_keys_list = list(each.keys())
        each_values_list = list(each.values())
        # print(each)
        # print(each_keys_list)
        # print(each_values_list)
        sheet_num = 0
        # 每一个循环建立一个新的标签,并在标签上填充数据(没必要的建立的直接跳过)
        for i in each_keys_list :
            # 针对"config"
            if type(each_values_list[sheet_num]) == type({1:2}):
                print("生成新标签")
                each_book_sheet = each_book.add_sheet("配置参数", cell_overwrite_ok=True)
                col_num = 0
                row_num = 0
                j_keys_list = list(each_values_list[sheet_num].keys())
                j_values_list = list(each_values_list[sheet_num].values())
                for x in j_keys_list :
                    each_book_sheet.write(row_num,col_num, label = x)
                    row_num += 1
                col_num = 1
                row_num = 0
                for y in j_values_list :
                    each_book_sheet.write(row_num,col_num, label = y)
                    row_num += 1
            # 针对"edges"和"fans"
            elif type(each_values_list[sheet_num]) == type(list("123")):
                print("生成新标签")
                if each_keys_list[sheet_num] == "edges":
                    sheet_name = "分支"
                if each_keys_list[sheet_num] == "fans":
                    sheet_name = "风机"
                each_book_sheet = each_book.add_sheet(sheet_name, cell_overwrite_ok=True)
                col_num = 0
                row_num = 0
                j_key_list=list(each_values_list[sheet_num][0].keys())
                j_list = list(each_values_list[sheet_num])
                for x in j_keys_list :
                    each_book_sheet.write(row_num,col_num, label = x)
                    col_num += 1
                col_num = 0
                row_num = 1
                dict_num = 0
                for y in j_list :
                    if dict_num <= len(j_list):
                        z_values_list = list(each_values_list[sheet_num][dict_num].values())
                        dict_num += 1
                    for v in z_values_list :
                        each_book_sheet.write(row_num,col_num, label = v)
                        col_num += 1
                    col_num = 0
                    row_num += 1
            else:
                pass
            sheet_num += 1
        
        each_book.save('%s.xls'%excel_name_list[excel_num])
        print("新的excel建立完成")
        excel_num += 1



def main():
    film_path = os.chdir("./data/json")
    Convert_to_excel(film_path)
if __name__ == '__main__':
    main()
    